import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/activity/activity_model.dart';

import '../../a_elder/models/elder_entity.dart';
import '../../a_elder/page/elder_search_page.dart';
import '../../net/response/activity/actvity_recommend_list_data.dart';
import 'activity_info_detal_view_model.dart';

class VolunteerSignInViewModel extends BaseViewModel {
  final ActvityRecommendListData item;

  VolunteerSignInViewModel(BuildContext context, this.item) : super(context);

  var contentList = <TitleValue>[];

  var mobile = "";
  ElderInfo? old;

  var model = ActivityModel();

  Future<void> selectOld() async {
    var data = await Go().push(ElderSearchPage(
      needBack: true,
    ));
    if (data != null) {
      old = data;
      mobile = old?.preMobile?.replaceAll("手机:", "")??"";
    }
    notifyListeners();
  }

  Future<void> submitEnroll() async {
    if (old == null) {
      showToast("请选择志愿者");
      return;
    }
    if (mobile.isNullOrEmpty()) {
      showToast("请输入联系方式");
      return;
    }
    showLoadingDialog();
    try {
      await model.enrollActivity(
          activityId: item.id.toString(),
          humanType: "1",
          regClientType: "windows",
          activityNo: item.activityNo??"",
          joinHumanId: old?.id.toString()??"",
          mobile: mobile,
          activityName: item.activityName??"");
      hideDialog();
      showToast("志愿者报名成功");
      finish();
    } catch (e) {
      showToast(NetErrorUtils.getNetError(e));
      hideDialog();
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    contentList = [
      TitleValue(title: "活动名称：", value: "${item.activityName}"),
      TitleValue(title: "活动内容：", value: "${item.activityContent}", type: 1),
      TitleValue(
        title: "活动类型：",
        value: "${item.activityTypeName}",
      ),
      TitleValue(
        title: "活动开始时间：",
        value: "${item.activityStartTime}",
      ),
      TitleValue(
        title: "活动结束时间：",
        value: "${item.activityEndTime}",
      ),
      TitleValue(
        title: "活动负责人：",
        value: "${item.directorName}",
      ),
      TitleValue(
        title: "报名开始时间：",
        value: "${item.enrollStartTime}",
      ),
      TitleValue(
        title: "报名结束时间：",
        value: "${item.enrollEndTime}",
      ),
      TitleValue(
        title: "签到积分：",
        value: "${item.checkPoints}",
      ),
      TitleValue(
        title: "活动人数：",
        value: "${item.isLimitHumans == 1 ? "${item.limitHumans}" : "不限制"}",
      ),
      TitleValue(
        title: "志愿者积分：",
        value: "${item.volunteerPoints}",
      ),
      TitleValue(
        title: "志愿者人数：",
        value: "${item.limitVolunteers}",
      ),
      TitleValue(title: "备注：", value: "${item.remark}", type: 1),
    ];
    notifyListeners();
  }
}
